package com.example.myson;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/3/9 0009.
 */
public class Dao extends ArrayAdapter<Msg> {
    private int resourceId;

    public Dao(Context context, int resource, List<Msg> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        this.resourceId = resource;

    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        Msg mMsg = getItem(position);
        View mView;
        ViewHolder mViewHolder;
        if(convertView == null)
        {
            mView = LayoutInflater.from(getContext()).inflate(resourceId, null);
            mViewHolder = new ViewHolder();
            mViewHolder.leftLayout = (LinearLayout) mView.findViewById(R.id.left_layout);
//            mViewHolder.rightLayout = (LinearLayout) mView.findViewById(R.id.right_layout);
//            mViewHolder.leftMsg = (TextView) mView.findViewById(R.id.text_viewleft1);
//            mViewHolder.rightMsg = (TextView) mView.findViewById(R.id.text_viewright1);
            mView.setTag(mViewHolder);
        }else
        {
            mView = convertView;
            mViewHolder = (ViewHolder) mView.getTag();
        }
        if(mMsg.getType() == Msg.TYPE_RECEIVED)
        {
            mViewHolder.leftLayout.setVisibility(View.VISIBLE);
            mViewHolder.rightLayout.setVisibility(View.GONE);
            mViewHolder.leftMsg.setText(mMsg.getContent());
        }else if(mMsg.getType() == Msg.TYPE_SENT)
        {
            mViewHolder.leftLayout.setVisibility(View.GONE);
            mViewHolder.rightLayout.setVisibility(View.VISIBLE);
            mViewHolder.rightMsg.setText(mMsg.getContent());
        }
        return mView;

    }

    class ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
    }
}